package com.xyhuo.leetcode;

import java.util.*;

public class FindKthLargest {

    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            if (priorityQueue.size() < k) {
                priorityQueue.add(nums[i]);
            } else {
                priorityQueue.add(nums[i]);
                priorityQueue.poll();
            }
        }
        int ans = priorityQueue.poll();

        return ans;
    }

    public static void main(String[] args) {
        FindKthLargest f = new FindKthLargest();
        int[] nums = {3, 2, 1, 5, 6, 4};
        f.findKthLargest(nums, 2);
    }

}
